package com.zbvc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zbvc.dao.AdminDao;
import com.zbvc.entity.Admin;
import com.zbvc.util.DBUtil;
/**
 * Admin业务
 * @author Administrator
 *
 */
public class AdminDaoImpl implements AdminDao {

	@Override
	public int insertAdmin(Admin admin) {
		
		Connection conn = DBUtil.getConnect();
		String sql = "INSERT INTO admin (username,password,name,email,phone,status,lend_num,max_num)"
				+ " VALUES (?,?,?,?,?,?,?,?)";
		int row=0;
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setString(1, admin.getUsername());
			pstm.setString(2, admin.getPassword());
			pstm.setString(3, admin.getName());
			pstm.setString(4, admin.getEmail());
			pstm.setString(5, admin.getPhone());
			pstm.setInt(6, 1);
			pstm.setInt(7, 100);
			pstm.setInt(8, 100);
			row = pstm.executeUpdate(); 
			return row;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return row;
	}

	@Override
	public Admin selectByNameAndPwd(String username, String password) {
		String sql = " SELECT * FROM admin WHERE username=? AND password=? ";
		Connection conn = DBUtil.getConnect();
		Admin admin = new Admin();
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setString(1, username);
			pstm.setString(2, password);
			ResultSet rs = pstm.executeQuery();
			if(rs.next()) {
				admin.setAid(rs.getInt("aid"));
				admin.setUsername(rs.getString("username"));
				admin.setName(rs.getString("name"));
				admin.setPassword(rs.getString("password"));
				admin.setEmail(rs.getString("email"));
				admin.setPhone(rs.getString("phone"));
				admin.setStatus(rs.getInt("status"));
				admin.setLend_num(rs.getInt("lend_num"));
				admin.setMax_num(rs.getInt("max_num"));
				return admin;	
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Admin getAdminById(Integer id) {
		
		String sql = " SELECT * FROM admin WHERE aid=?";
		Connection conn = DBUtil.getConnect();
		Admin admin = new Admin();
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setInt(1, id);
			ResultSet rs = pstm.executeQuery();
			if(rs.next()) {
				admin.setAid(rs.getInt("aid"));
				admin.setUsername(rs.getString("username"));
				admin.setName(rs.getString("name"));
				admin.setPassword(rs.getString("password"));
				admin.setEmail(rs.getString("email"));
				admin.setPhone(rs.getString("phone"));
				admin.setStatus(rs.getInt("status"));
				admin.setLend_num(rs.getInt("lend_num"));
				admin.setMax_num(rs.getInt("max_num"));
				return admin;	
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int updatePassword(Integer aid, String newPwd) {
		Connection conn = DBUtil.getConnect();
		String sql = "UPDATE admin SET password=? WHERE aid=?";
		PreparedStatement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, newPwd);
			stm.setInt(2, aid);
			int row = stm.executeUpdate();
			return row;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			// 关闭连接资源
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return 0;
	}

	@Override
	public int updateAdminByAid(Admin admin) {
		String sql = "UPDATE admin SET name=?,email=?,phone=? WHERE aid=?";
		Connection conn = DBUtil.getConnect();
		PreparedStatement stm = null;
		ResultSet rs = null;
		int row = 0;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, admin.getName());
			stm.setString(2, admin.getEmail());
			stm.setString(3, admin.getPhone());
			stm.setInt(4, admin.getAid());
			row = stm.executeUpdate();
			return row;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row;
	}

	@Override
	public List<Admin> findAllAdmin(int status) {
		String sql = " SELECT * FROM admin WHERE status=?";
		Connection conn = DBUtil.getConnect();
		List<Admin> lists = new ArrayList<>();
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setInt(1, status);
			ResultSet rs = pstm.executeQuery();
			while(rs.next()) {
				Admin admin = new Admin();
				admin.setAid(rs.getInt("aid"));
				admin.setUsername(rs.getString("username"));
				admin.setName(rs.getString("name"));
				admin.setPassword(rs.getString("password"));
				admin.setEmail(rs.getString("email"));
				admin.setPhone(rs.getString("phone"));
				admin.setStatus(rs.getInt("status"));
				admin.setLend_num(rs.getInt("lend_num"));
				admin.setMax_num(rs.getInt("max_num"));
				lists.add(admin);	
			}
			return lists;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int updateUserById(Admin admin) {
		Connection conn = DBUtil.getConnect();
		String sql = " UPDATE admin SET NAME=? ,username=?,email=?,phone=?, " + 
				" password=?,max_num=?,lend_num=? WHERE aid=?";
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setString(2, admin.getUsername());
			pstm.setString(5, admin.getPassword());
			pstm.setString(1, admin.getName());
			pstm.setString(3, admin.getEmail());
			pstm.setString(4, admin.getPhone());
			pstm.setInt(7, admin.getLend_num());
			pstm.setInt(6, admin.getMax_num());
			pstm.setInt(8, admin.getAid());
			int row = pstm.executeUpdate();
			return row;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	

}
